package com.homework9;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        new Thread() {
            @Override
            public void run() {
                try {
                    while (true) {
                        System.out.println("等待新客户端下载文件!");
                        Socket socket = serverSocket.accept();
                        System.out.println(serverSocket.getInetAddress().getHostAddress() + "连接成功");
                        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\admin\\Desktop\\1610359154226.jpg")));
                        final OutputStream outputStream = socket.getOutputStream();
                        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
                        int len;
                        byte[] bytes = new byte[8192];
                        while ((len = bufferedInputStream.read(bytes)) != -1) {
                            bufferedOutputStream.write(bytes, 0, len);
                        }
                        bufferedOutputStream.close();
                        System.out.println("文件下载成功....");
                        bufferedInputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
